package com.poe.telltaleheart;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;

public class TellTaleHeart extends Game implements Constants,InputProcessor {
    OrthographicCamera camera;

    SpriteBatch batch;
    Texture map;
    Rectangle mapRect;

    int mouseDownX, mouseDownY;
    private int screenWidth;
    private int screenHeight;
    private int currentMouseX;
    private int lastMouseX;
    private int currentMouseY;
    private int lastMouseY;
    private boolean canBeTouched;

    @Override
    public void create() {


        map = new Texture(Gdx.files.internal("map.jpg"));
        mapRect = new Rectangle();
        mapRect.width = map.getWidth();
        mapRect.height = map.getHeight();

        batch = new SpriteBatch();
        Gdx.input.setInputProcessor(this)  ;

    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        //handleInput();

        // cameraLimits();
        camera.update();

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(map, 0, 0);
        batch.end();


    }
    private void cameraLimits(){
        if (camera.position.x < (screenWidth / 2)) {
            camera.position.x = screenWidth / 2;
        }
        if (camera.position.y < (screenHeight / 2)) {
            camera.position.y = screenHeight / 2;
        }
        if (camera.position.x > (screenWidth * 2)) {
            camera.position.x = screenWidth * 2;
        }

        if (camera.position.y > (screenHeight * 2)) {
            camera.position.y = screenHeight * 2;
        }
    }
     private void handleInput(){
         if (Gdx.input.justTouched()) {
             lastMouseX = Gdx.input.getX();
             lastMouseY = Gdx.input.getY();
         } else if (Gdx.input.isTouched()) {
             currentMouseX = Gdx.input.getX();
             currentMouseY = Gdx.input.getY();
             camera.translate(lastMouseX - currentMouseX, currentMouseY - lastMouseY);
             lastMouseX = currentMouseX;
             lastMouseY = currentMouseY;
             // Gdx.app.log("MyTag", "cameraX" + camera.position.x + "  screenWidth:" + screenWidth);
         }
     }

    @Override
    public void resize(int width, int height) {
        screenWidth = width;
        screenHeight = height;
        camera = new OrthographicCamera();
        camera.setToOrtho(false, screenWidth, screenHeight);

    }

    private void animateTo() {

    }


        @Override
        public boolean keyDown(int keycode) {
            return false;
        }

        @Override
        public boolean keyUp(int keycode) {
            return false;
        }

        @Override
        public boolean keyTyped(char character) {
            return false;
        }

        @Override
        public boolean touchDown(int x, int y, int pointer, int button) {
            if (button==   Input.Buttons.LEFT){
            lastMouseX = x;
            lastMouseY = y;
            return true;
            }
            return false;
        }

        @Override
        public boolean touchUp(int x, int y, int pointer, int button) {
            return false;
        }

        @Override
        public boolean touchDragged(int x, int y, int pointer) {
            currentMouseX =x;
            currentMouseY =y;
            camera.translate(lastMouseX - currentMouseX, currentMouseY - lastMouseY);
            lastMouseX = currentMouseX;
            lastMouseY = currentMouseY;
            return false;
        }

        @Override
        public boolean mouseMoved(int x, int y) {
            return false;
        }

        @Override
        public boolean scrolled(int amount) {
            camera.zoom+=amount/10f;
            return false;
        }

}